import USART
import time
import cv2
import struct
import all_socket
import pickle
import threading
import GPS_SEND
import camera

# 请根据实际情况修改串口号
sender = USART.SerialSender("/dev/ttyS3", 9600)
print("/dev/ttyS3;9600")
received_array=[1,1,1,1,1,1]

all_socket.message_socket1()
all_socket.camera_socket2()


def GPS_datasend():
  GPS_SEND.GPS_datasend()


def sendcamera():
  camera.camera_send()


def recieve_car_message():
  while True:
    # 接收数据
    received_data =all_socket.message_socket.recv(1024).decode("UTF-8")
    # 将接收到的字节流反序列化为数组
    print(received_data)
    print(type(received_data))
    
    received_array1=received_data.split(",")
    received_array[0] = int(received_array1[0],16)
    received_array[1] = int(received_array1[1],16)
    received_array[2] = int(received_array1[2],16)
    received_array[3] = int(received_array1[3],16)
    received_array[4] = int(received_array1[4],16)
    received_array[5] = int(received_array1[5],16)
    print(received_array)
    print(type(received_array[0]))
    print(type(received_array))
    
    if received_array[1]==0x01:
      sender.air_speed(received_array)
      print("speed:%d.%d"%(received_array[2],received_array[3]))
    elif received_array[1]==0x02:
      sender.air_distance(received_array)
      print("distance:%d.%d"%(received_array[2],received_array[3]))
    elif received_array[1]==0x04:
      sender.air_armmotion(received_array) 
  # 关闭连接
  all_socket.message_socket.close()
  sender.close_serial()


if __name__ == '__main__':
  #recieve_car_message()
  recv_data_thread = threading.Thread(target=recieve_car_message)
  #send_gps_thread = threading.Thread(target=GPS_datasend)
  send_cemera_thread = threading.Thread(target=sendcamera)
  recv_data_thread.start()
  send_cemera_thread.start()
  #send_gps_thread.start()
